<?php
namespace app\frontend\forms;

use Phalcon\Forms\Element;
use Phalcon\Forms\Form;
use app\frontend\forms\elements\SelectUlElement;
use Phalcon\Forms\Element\Text;
use app\models\Ins;
use Phalcon\Db\Column;

class BaseInsSearchForm extends Form
{
    public $searchInsName;
    public $searchInsStatus;
    public $searchInsBrand;
    public $searchManageDepart;
    public $searchInsPrecision;
    public $searchManufactures;
    public $searchNorms;
    public $searchInsGrade;

    public function initialize()
    {
        $this->setEntity($this);

        $this->addTheElement(new SelectUlElement('searchInsName', ['placeholder'=>'仪器名称', 'class'=>'form-control']),
            '仪器名称',
            ['models'=>\app\models\BaseInsName::find()]
        );

        $this->addTheElement(new SelectUlElement('searchInsStatus',['placeholder'=>'仪器状态', 'class'=>'form-control']),
            '仪器状态',
            ['models'=>\app\models\BaseInsStatus::find()]
        );

        $this->addTheElement(new SelectUlElement('searchInsBrand',['placeholder'=>'仪器品牌', 'class'=>'form-control']),
            '仪器品牌',
            ['models'=>\app\models\BaseInsBrand::find()]
        );

        $this->addTheElement(new SelectUlElement('searchManageDepart',['placeholder'=>'管理部门','class'=>'form-control']),
            '管理部门',
            ['models'=>\app\models\BaseManageDepartment::find()]
        );

        $this->addTheElement(new SelectUlElement('searchInsPrecision',['placeholder'=>'仪器精度','class'=>'form-control']),
            '仪器精度',
            ['models'=>\app\models\BaseInsPrecision::find()]
        );

        $this->addTheElement(new SelectUlElement('searchManufactures',['placeholder'=>'生产厂家','class'=>'form-control']),
            '生产厂家',
            ['models'=>\app\models\BaseManufacturers::find()]
        );

        $this->addTheElement(new Text('searchNorms',['placeholder'=>'型号规格','class'=>'form-control']),'型号规格');

        $this->addTheElement(new SelectUlElement('searchInsGrade',['placeholder'=>'仪器等级','class'=>'form-control']),
            '仪器等级',
            ['models'=>\app\models\BaseManageDepartment::find()]
        );
    }

    /**
     * @param \Phalcon\Mvc\Model\Criteria|null $query
     * @return \Phalcon\Mvc\Model\Criteria
     */
    public function getQuery(\Phalcon\Mvc\Model\Criteria $query = null)
    {
        $query = $query?$query:Ins::query();
        if($this->searchInsName)
            $query->andwhere("ins_name like :searchInsName:",['searchInsName'=>'%'.$this->searchInsName.'%'],['searchInsName'=>Column::BIND_PARAM_STR]);
        if($this->searchInsStatus)
            $query->andwhere("ins_status like :searchInsStatus:",['searchInsStatus'=>'%'.$this->searchInsStatus.'%'],['searchInsStatus'=>Column::BIND_PARAM_STR]);
        if($this->searchInsBrand)
            $query->andwhere("ins_brand like :searchInsBrand:",['searchInsBrand'=>'%'.$this->searchInsBrand.'%'],['searchInsBrand'=>Column::BIND_PARAM_STR]);
        if($this->searchManageDepart)
            $query->andwhere("manage_depart like :searchManageDepart:",['searchManageDepart'=>'%'.$this->searchManageDepart.'%'],['searchManageDepart'=>Column::BIND_PARAM_STR]);
        if($this->searchInsPrecision)
            $query->andwhere("ins_precision like :searchInsPrecision:",['searchInsPrecision'=>'%'.$this->searchInsPrecision.'%'],['searchInsPrecision'=>Column::BIND_PARAM_STR]);
        if($this->searchManufactures)
            $query->andwhere("manufactures like :searchManufactures:",['searchManufactures'=>'%'.$this->searchManufactures.'%'],['searchManufactures'=>Column::BIND_PARAM_STR]);
        if($this->searchNorms)
            $query->andwhere("norms like :searchNorms:",['searchNorms'=>'%'.$this->searchNorms.'%'],['searchNorms'=>Column::BIND_PARAM_STR]);
        if($this->searchInsGrade)
            $query->andwhere("ins_grade like :searchInsGrade:",['searchInsGrade'=>'%'.$this->searchInsGrade.'%'],['searchInsGrade'=>Column::BIND_PARAM_STR]);
        return $query;
    }

    protected function addTheElement(Element $element,$label = null,$userOptions = []){
        if(!empty($userOptions)){
            $element->setUserOptions($userOptions);
        }
        if($label){
            $element->setLabel($label);
        }
        $this->add($element);
    }
}